<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="listQuery.title" placeholder="标题" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
      
      <el-select v-model="listQuery.type" placeholder="类型" clearable class="filter-item" style="width: 130px">
        <el-option v-for="item in calendarTypeOptions" :key="item.key" :label="item.display_name+'('+item.key+')'" :value="item.key" />
      </el-select>
     
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        搜索
      </el-button>
        <router-link :to="{path:'/project/projectInfo'}">
              <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-edit" >
            添加
              </el-button>
        </router-link>
    </div>
    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column label="编号"  align="center" width="60" >
        <template slot-scope="scope">
          <span>  {{ scope.$index+1 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" width="160px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.createTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="标题" min-width="200px"> 
        <template slot-scope="{row}">
          <span class="link-type" >{{ row.title }}</span>
          <el-tag v-if="row.type ==1" type="success" effect="dark">股权设立</el-tag>
          <el-tag v-else-if="row.type ==2" effect="dark" >股权变更</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="首页图片" width="120px" align="center">
        <template slot-scope="{row}">
           <el-image
                style="width: 100px; height: 60px"
                :src="row.pic"
                fit="contain"
            ></el-image>
        </template>
      </el-table-column>
      <el-table-column label="简介" min-width="110px" align="center">
        <template slot-scope="{row}">
          <span >{{ row.contentInfo }}</span>
        </template>
      </el-table-column>
      <el-table-column label="联系人" width="80px" align="center">
        <template slot-scope="{row}">
          <span >{{ row.projectContacts }}</span>
        </template>
      </el-table-column>
      <el-table-column label="联系人电话" width="100px" align="center">
        <template slot-scope="{row}">
          <span >{{ row.contactsPhone }}</span>
        </template>
      </el-table-column>
     
      <!-- <el-table-column label="状态" class-name="status-col" width="100">
        <template slot-scope="{row}">
            <el-tag v-if="row.status ==1" type="success" >通过</el-tag>
          <el-tag v-else-if="row.status ==2" type="danger"  >已拒绝</el-tag>
          <el-tag v-else  >待审核</el-tag>
        </template>
      </el-table-column> -->
      <el-table-column label="操作" align="center" width="230" class-name="small-padding fixed-width">
        <template slot-scope="{row,$index}">
           <el-dropdown  >
            <el-button type="primary" size="mini">
              更多<el-icon class="el-icon--right"><arrow-down /></el-icon>
            </el-button>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item command="1">
                  <router-link :to="{path:'/project/projectFolder',query:{id:row.pid}}">
                  项目资料
                </router-link>
                </el-dropdown-item>
                 <el-dropdown-item command="1">
                  <router-link :to="{path:'/project/projectAdd',query:{id:row.pid,type:'youshi'}}">
                  项目优势
                </router-link>
                </el-dropdown-item>
                 <el-dropdown-item command="1">
                  <router-link :to="{path:'/project/projectAdd',query:{id:row.pid,type:'fangan'}}">
                  投资方案
                </router-link>
                </el-dropdown-item>
                 <el-dropdown-item command="1">
                  <router-link :to="{path:'/project/projectAdd',query:{id:row.pid,type:'quanyi'}}">
                  项目权益
                </router-link>
                </el-dropdown-item>
               
              </el-dropdown-menu>
            </template>
          </el-dropdown>
          <router-link :to="{path:'/project/projectEdit',query:{id:row.pid}}">
                <el-button type="primary" size="mini" >
                  编辑
                </el-button>
                </router-link>
         
          <el-popconfirm
              confirm-button-text="是"
              cancel-button-text="取消"
              icon-color="red"
              title="你是否确定删除此数据?"
                @onConfirm="handleDelete(row,$index)"
            >
              <template #reference>
                <el-button size="mini" type="danger">删除</el-button>
              </template>
            </el-popconfirm>
      
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />

  </div>
</template>

<script>
import { GQL } from '@/api/article'
import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination' // secondary package based on el-pagination


const calendarTypeOptions = [
  { key: '1', display_name: '股权设立' },
  { key: '2', display_name: '股权变更' }
]

// arr to obj, such as { CN : "China", US : "USA" }
const calendarTypeKeyValue = calendarTypeOptions.reduce((acc, cur) => {
  acc[cur.key] = cur.display_name
  return acc
}, {})

export default {
  name: 'ComplexTable',
  components: { Pagination },
  directives: { waves },

  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        importance: undefined,
        title: undefined,
        type: undefined,
        sort: '+id'
      },
      calendarTypeOptions,
      showReviewer: false,
   
    }
  },
  created() {
     this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      let where = {}
        if(this.listQuery.page != 1){
             where = {"filter": {"isDelete": {"equalTo": "0"},"status":{"equalTo": "1"}}, "first": this.listQuery.limit, "offset": this.listQuery.page*10-10, "orderBy": "CREATE_TIME_DESC"}
        }else{
         where = {"filter": {"isDelete": {"equalTo": "0"},"status":{"equalTo": "1"}}, "first": this.listQuery.limit, "offset": this.listQuery.page-1, "orderBy": "CREATE_TIME_DESC"}
        }
        if(this.listQuery.title != undefined ){
            where.filter.title = {"like":"%"+this.listQuery.title+"%"}
        }
        if(this.listQuery.type != undefined ){
            where.filter.type = {"equalTo": this.listQuery.type}
        }
        const query = {
               "operationName": "allSlProjects",
"query": "query allSlProjects($filter: SlProjectFilter, $offset: Int, $first: Int, $orderBy: [SlProjectsOrderBy!] = [PRIMARY_KEY_ASC]) { allSlProjects( filter: $filter first: $first offset: $offset orderBy: $orderBy ) { totalCount nodes { title type status pic pid uuid createTime contentInfo projectContacts contactsPhone projectContent } } }",
"variables":where
        }
       GQL(query).then(response => {
        this.list = response.data.allSlProjects.nodes
        this.total = response.data.allSlProjects.totalCount
        setTimeout(() => {
          this.listLoading = false
        }, 1.5 * 1000)
      })
      
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleDelete(row, index) {
          const data = {
              isDelete:'1'
          }
          let query = {
            "operationName": "updateSlProjectByPid",
            "query": "mutation updateSlProjectByPid($input: UpdateSlProjectByPidInput!) { updateSlProjectByPid(input: $input) { slProject { pid title } } }",
            "variables": {input: {pid: row.pid, slProjectPatch:data}}
          }
          GQL(query).then((res) => {
               if(res.errors){
                  this.$notify({
                    title: '删除失败',
                    message: '删除项目失败',
                    type: 'error',
                    duration: 2000
                  })
              }else{
                  this.$notify({
                    title: '删除成功',
                    message: '删除项目成功',
                    type: 'success',
                    duration: 2000
                  })
                  this.list.splice(index, 1)
              }
      
          })
        
    }
  }
}
</script>
<style scoped>
.demonstration {
  display: block;
  color: var(--el-text-color-secondary);
  font-size: 14px;
  margin-bottom: 20px;
}
 .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 100px;
    height: 100px;
    line-height: 100px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
